var express = require('express');
var router = express.Router();
var mysql = require('mysql');


var config = require('../../config/config.js');
var db = mysql.createPool({ host: config.host, user: config.user, password: config.password, database: config. database})


router.get('/', function(req, res) {
    res.send('api')
})

router.get('/getData', function(req, res) {

    var type = req.query.type || '';
    var page = req.query.page || 1;
    var where = ' WHERE category="' + type + '"'; //查询条件

    // 判断是否传入类型
    if (type == '') {
        where = '';
    }
    console.log(where)
    db.query('SELECT *FROM zq_article ' + where + ' ORDER BY post_time DESC', function(err, data) {
        if (err) {
            console.log(err);
            res.status(500).send('database error').end();
        } else {
            res.json(data);
        }
    })


    // res.send('api')
})

router.use('/postData', function(req, res) {

    db.query('SELECT COUNT(id) AS count FROM zq_article', function(err, data) {
        if (err) {
            console.log(err);
            res.status(500).send('database error').end();
        } else {
            res.json(data[0]);
        }
    })


    // res.send('post')
})




// 获取文章总条数
router.get('/get_article_count', function(req, res) {
    db.query('SELECT COUNT(id) AS count FROM zq_article', function(err, data) {
        if (err) {
            console.log(err);
            res.status(500).send('database error').end();
        } else {
            res.json(data[0]);
        }
    })

})


router.post('/post_article_count', function(req, res) {
    console.log(req.body)
    db.query('SELECT COUNT(id) AS count FROM zq_article', function(err, data) {
        if (err) {
            console.log(err);
            res.status(500).send('database error').end();
        } else {
            res.json(data[0]);
        }
    })

})

// 获取文章列表
router.get('/get_article_list', function(req, res) {
    console.log(req.query)
    var type = req.query.type || '';
    var page = req.query.page || 1;
    var where = ' WHERE category="' + type + '"'; //查询条件

    // 判断是否传入类型
    if (type == '') {
        where = '';
    }
    console.log(where)
    db.query('SELECT *FROM zq_article ' + where + ' ORDER BY post_time DESC', function(err, data) {
        if (err) {
            console.log(err);
            res.status(500).send('database error').end();
        } else {
            res.json(data);
        }
    })
})

// 获取标签名
router.get('/get_article_tag', function(req, res) {
    db.query('SELECT *FROM zq_category WHERE showTag="1" ORDER BY orderTag ASC', function(err, data) {
        if (err) {
            console.log(err);
            res.status(500).send('database error').end();
        } else {
            res.json(data);
        }
    })
})


router.get('/get', function(req, res) {
    var dataArr = [];
    db.query('SELECT *FROM zq_article ORDER BY post_time DESC', function(err, data) {
        if (err) {
            console.log(err);
            res.status(500).send('database error').end();
        } else {
            dataArr = data;
            dataArr.forEach(function(el, index) {
                (function() {

                    var i = index;
                    var obj = el;
                    db.query('SELECT id,category FROM zq_category WHERE zq_category.id in(' + obj.category + ')', function(err, newData) {

                        if (err) {
                            console.log(err);
                            res.status(500).send('database error').end();
                        } else {
                            dataArr[i].category = newData;
                            if (i == dataArr.length - 1) {
                                res.json(dataArr);
                            }
                        }
                    })
                })();

            });
        }
    })
})




router.get('/get_article', function(req, res) {

    var items = {};

    // 获取文章
    function getArticle() {
        return new Promise(function(resolve, reject) {
            var dataArr = [];
            db.query('SELECT *FROM zq_article ORDER BY post_time DESC', function(err, data) {
                if (err) {
                    reject(err);
                } else {
                    dataArr = data;
                    dataArr.forEach(function(el, index) {
                        (function() {

                            var i = index;
                            var obj = el;
                            db.query('SELECT id,category FROM zq_category WHERE zq_category.id in(' + obj.category + ')', function(err, newData) {

                                if (err) {
                                    reject(err);
                                } else {
                                    dataArr[i].category = newData;
                                    if (i == dataArr.length - 1) {

                                        resolve(dataArr)

                                    }
                                }
                            })
                        })();
                    });
                }
            })
        })
    }

    function getTag() {
        return new Promise(function(resolve, reject) {
            db.query('SELECT *FROM zq_category WHERE showTag="1" ORDER BY orderTag', function(err, data) {
                if (err) {
                    reject(err);
                } else {
                    resolve(data)
                }
            })
        })
    }

    getArticle().then(function(data) {
        items.articles = data;
        return getTag()

    }, function(err) {
        console.log(err);
        res.status(500).send('database error').end();
    }).then(function(data) {

        items.tags = data;
        console.log(items)

        res.send(dataArr).end();
        // res.render('home/index', {
        //     items: items
         // })
    }, function(err) {
        console.log(err);
        res.status(500).send('database error').end();
    })

})

module.exports = router
